package com.ksp.util;

import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import org.mockito.asm.tree.IntInsnNode;


/**
 * 文件操作工具类
 * @author anye
 *
 */
public class FileUtil {
	
	/**
	 * 获取指定文件目录下的所有文件名（java类名）集合
	 */
	public static List<String> getClassNameLst(String filPth, List<String> classname) {
		List<String> wClassNam = new ArrayList<>();
		File file = new File(filPth);
		File[] childFils = file.listFiles();
		for (File childFil : childFils) {
			String wChdPth = childFil.getPath();
			if (childFil.isDirectory()) {
				wClassNam.addAll(getClassNameLst(wChdPth, wClassNam));
			} else {
				
				wChdPth = wChdPth.substring(wChdPth.indexOf("main\\java") + 10, wChdPth.lastIndexOf("."));
				wChdPth = wChdPth.replace("\\", ".");
				wClassNam.add(wChdPth);
			}
		}
		
		return wClassNam;
	}
	
	public static List<String> getClassName(String filePath, List<String> className) {  
		List<String> myClassName = new ArrayList<String>();  
		File file = new File(filePath);  
		File[] childFiles = file.listFiles();  
		for (File childFile : childFiles) {  
			if (childFile.isDirectory()) {  
				myClassName.addAll(getClassName(childFile.getPath(), myClassName));  
			} else {  
				String childFilePath = childFile.getPath();  
				childFilePath = childFilePath.substring(childFilePath.indexOf("\\classes") + 9, childFilePath.lastIndexOf("."));  
				childFilePath = childFilePath.replace("\\", ".");  
				myClassName.add(childFilePath);  
			}  
		}  

		return myClassName;  
	}  
	
	
	public static List<String> FilStrLst = new ArrayList<>();

	public static void getFileLst(String path) {
		
		File file = new File(path);
		if (file.isDirectory()) {
			File[] files = file.listFiles();
			for (int i = 0; i < files.length; i++) {
				File childfile = files[i];
				if (childfile.isDirectory()) {
					getFileLst(childfile.getPath());
				} else {
					String wEndPth = childfile.getPath();
					//System.out.println(wEndPth);
					FilStrLst.add(wEndPth);
				}
				
			}
		} else {
			String wEndPth = file.getPath();
			//System.out.println(wEndPth);
			FilStrLst.add(wEndPth);
		}
	}
	
	
	public static void getFiles(String path) {
        File file = new File(path);
        // 如果这个路径是文件夹
        if (file.isDirectory()) {
            // 获取路径下的所有文件
            File[] files = file.listFiles();
            for (int i = 0; i < files.length; i++) {
                // 如果还是文件夹 递归获取里面的文件 文件夹
                if (files[i].isDirectory()) {
                    System.out.println("目录：" + files[i].getPath());
                    getFiles(files[i].getPath());
                } else {
                    System.out.println("文件：" + files[i].getPath());
                }
 
            }
        } else {
            System.out.println("文件：" + file.getPath());
        }
    }
	

}
